import numpy as np

# 创建一个由等差数列组成的数组
'''
numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None)
其中：start表示起始点，stop表示终止点，endpoint为True则包含终止点，为False则不包含；retstep如果为True
则返回（样本，步长）；dtype表示输出的数据类型
'''

arr = np.linspace(1, 10, 10)
print(arr)

arr1 = np.linspace(1, 10, 10, endpoint=False)
print(arr1)
a = np.linspace(1, 10, 10, endpoint=False, retstep=True)
print(a)

arr2 = np.array([1, 2, 3, 4, 5])
print(arr2)
print(arr2.dtype)
# 转换数据类型：init64 -> float64
float_arr = arr2.astype(np.float64)
print(float_arr.dtype)

arrA = np.array([1, 2, 3])
arrB = np.array([1, 2.2, 3])  # 自动转换为浮点数
arrC = np.array([1, 2.2, '3'])  # 自动转换为字符串
print(arrA)
print(arrB)
print(arrC)

# 对数组进行减法运算
t1 = np.arange(12).reshape(4, 3)
t2 = np.arange(3)
t3 = t1 - t2
print(t1)
print(t2)
print(t3)

# 学习NumPy的取整函数
np.random.seed(7)  # np.random.seed用随机数种子使每次生成的随机树相同
a = np.random.randn(5)  # 返回符合标准正态分布的一组值
a_a = np.around(a, 2)  # 四舍五入
a_f = np.floor(a)  # 向下取整
a_c = np.ceil(a)  # 向上取整
print("a:", a)
print("a_a:", a_a)
print("a_f:", a_f)
print("a_c:", a_c)
print("--------------------")
# 广播Boradcast
a = np.arange(1, 10).reshape(3, 3)
b = np.array([1, 1, 1])
c = a + b  # 广播
d = np.add(a, b)  # add(),subtract(),multiply(),divide()函数可对数组使用广播进行加减乘除运算
print(c)
print(d)
print("--------------------")
# 两个数组（矩阵）点乘
a = np.array([2, 4, 5])
b = np.array([[1], [1], [1]])  # 也可用语句 numpy.array([1,1,1])
c = np.dot(a, b)
print(c)
print("--------------------")
# 一维数组切片和索引
a = np.arange(9)
print("a:", a)
print("a[3]:", a[3])
print("a[-1]:", a[-1])
print("a[1:6:3]:", a[1:6:3])
print("a[:]:", a[:])
print("a[::-1]:", a[::-1])
print("--------------------")
# 二维数组切片和索引
a = np.arange(1, 13).reshape(3, 4)
print(a)
print(a[2])
print(a[1][3])
print(a[1, 3])
print(a[:, 1])
print(a[::2, :])
print(a[::-1, ::-1])
print("--------------------")
# 多维数组切片和索引
ar_2 = np.arange(12).reshape(3, 4)
print("ar_2:", ar_2)
print(ar_2[2])
ar_3 = np.arange(12).reshape(3, 2, 2)
print("ar_3:", ar_3)
old = ar_3[0].copy()
ar_3[0] = 100
print(ar_3)
print("\n")
ar_3[0] = old
print("ar_3:", ar_3)
print(ar_3[1, 1])
print(ar_3[1, 1, 1])
